﻿using System;
using System.IO;
using Windows.Storage;

namespace DNFImagePacks2Manager.Helpers
{
    // 本地配置工具
    public sealed class LocalStorageHelper
    {
        // 配置键
        private static class Keys
        {
            // 主题
            public const string AppTheme = "AppTheme";
            // 窗体宽度
            public const string WindowWidth = "WindowWidth";
            // 窗体高度
            public const string WindowHeight = "WindowHeight";
            // DNF路径
            public const string DNFPath = "DNFPath";
            // 图像补丁文件夹名
            public const string ImageFolderName = "ImagePacks2";
            // 音效补丁文件夹名
            public const string SoundFolderName = "SoundPacks";
            // BK补丁文件夹名
            public const string BKFolderName = "Video";
            // BK补丁缓存路径
            public const string BKPath = "BKPath";
            // 预览音频数量
            public const string PreviewSoundCount = "PreviewSoundCount";
            // 预览图片数量
            public const string PreviewImageCount = "PreviewCount";
            // 预览点阵图最小宽度
            public const string PreviewWidth = "PreviewWidth";
            // 预览点阵图最小高度
            public const string PreviewHeight = "PreviewHeight";
            // 是否预览V4图片
            public const string PreviewV4 = "PreviewV4";
            // 每个IMG文件读取图片数量
            public const string PreviewPerCount = "PreviewPerCount";
            // 是否自动检查更新
            public const string AutoUpdateCheck = "AutoCheckUpdate";
            // 图片缩放插值算法
            public const string InterpolationMode = "InterpolationMode";
            // 缩放启用高斯模糊
            public const string GaussianBlur = "GaussianBlur";
        }

        // 配置默认值
        private static class Defaults
        {
            public const string AppTheme = "使用系统设置";
            public const int WindowWidth = 0;
            public const int WindowHeight = 0;
            public const string DNFPath = "未选择路径";
            public const int PreviewCount = 5;
            public const int PreviewWidth = 25;
            public const int PreviewHeight = 25;
            public const bool PreviewV4 = true;
            public const int PreviewPerCount = 3;
            public const bool AutoUpdateCheck = false;
            public const int InterpolationMode = 2;
            public const bool GaussianBlur = true;

            public static string BKPath => Path.Combine(FileHelper.GetAppFolder, "bk");
        }

        // 缓存LocalSettings实例
        private static readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;

        // 主题
        public static string GetAppTheme() => GetValue(Keys.AppTheme, Defaults.AppTheme);
        public static bool SetAppTheme(string value) => SetValue(Keys.AppTheme, value);

        // 窗口大小
        public static int GetWindowWidth() => GetValue(Keys.WindowWidth, Defaults.WindowWidth);
        public static int GetWindowHeight() => GetValue(Keys.WindowHeight, Defaults.WindowHeight);
        public static bool GetWindowSize(out int width, out int height)
        {
            width = GetWindowWidth();
            height = GetWindowHeight();
            return width > 100 && height > 100;
        }
        public static bool SetWindowWidth(int width) => SetValue(Keys.WindowWidth, width);
        public static bool SetWindowHeight(int height) => SetValue(Keys.WindowHeight, height);
        public static bool SetWindowSize(int width, int height) => SetWindowWidth(width) && SetWindowHeight(height);

        // DNF官方文件夹
        public static string GetDNFPath() => GetValue(Keys.DNFPath, Defaults.DNFPath);
        public static bool SetDNFPath(string value)
        {
            string imagePath = Path.Combine(value, Keys.ImageFolderName);
            if (Directory.Exists(imagePath))
            {
                return SetValue(Keys.DNFPath, value);
            }
            return false;
        }
        public static bool IsSetDNFPath()
        {
            string dnfPath = GetDNFPath();
            return !dnfPath.Equals(Defaults.DNFPath, StringComparison.Ordinal)
                   && Directory.Exists(GetImagePath());
        }
        public static string GetImagePath() => GetSubPath(Keys.ImageFolderName);
        public static string GetSoundPath() => GetSubPath(Keys.SoundFolderName);
        public static string GetBKOfficialPath() => GetSubPath(Keys.BKFolderName);
        private static string GetSubPath(string subFolder)
        {
            string dnfPath = GetDNFPath();
            return dnfPath.Equals(Defaults.DNFPath, StringComparison.Ordinal)
                ? string.Empty
                : Path.Combine(dnfPath, subFolder);
        }

        // BK缓存文件夹
        public static string GetBKStoragePath() => GetValue(Keys.BKPath, Defaults.BKPath);
        public static bool SetBKStoragePath(string path)
        {
            if (Directory.Exists(path) && BKHelper.ChangeBKStoragePath(path))
            {
                return SetValue(Keys.BKPath, path);
            }
            return false;
        }

        // 补丁预览配置
        public static int GetNPKPreviewSoundCount() => GetValue(Keys.PreviewSoundCount, Defaults.PreviewCount);
        public static bool SetNPKPreviewSoundCount(int count) => SetValue(Keys.PreviewSoundCount, count);
        public static int GetNPKPreviewImageCount() => GetValue(Keys.PreviewImageCount, Defaults.PreviewCount);
        public static bool SetNPKPreviewImageCount(int count) => SetValue(Keys.PreviewImageCount, count);
        public static int GetNPKPreviewMinWidth() => GetValue(Keys.PreviewWidth, Defaults.PreviewWidth);
        public static int GetNPKPreviewMinHeight() => GetValue(Keys.PreviewHeight, Defaults.PreviewHeight);
        public static bool SetNPKPreviewMinWidth(int width) => SetValue(Keys.PreviewWidth, width);
        public static bool SetNPKPreviewMinHeight(int height) => SetValue(Keys.PreviewHeight, height);
        public static bool SetNPKPreviewMinSize(int width, int height) => SetNPKPreviewMinWidth(width) && SetNPKPreviewMinHeight(height);
        public static bool GetNPKPreviewEnableV4() => GetValue(Keys.PreviewV4, Defaults.PreviewV4);
        public static bool SetNPKPreviewEnableV4(bool value) => SetValue(Keys.PreviewV4, value);
        public static int GetNPKPreviewPerCount() => GetValue(Keys.PreviewPerCount, Defaults.PreviewPerCount);
        public static bool SetNPKPreviewPerCount(int count) => SetValue(Keys.PreviewPerCount, count);

        // 小工具配置
        public static int GetInterpolationModeCode() => GetValue(Keys.InterpolationMode, Defaults.InterpolationMode);
        public static bool SetInterpolationModeCode(int code) => SetValue(Keys.InterpolationMode, code);
        public static InterpolationMode GetInterpolationMode()
        {
            int code = GetInterpolationModeCode();
            return Enum.IsDefined(typeof(InterpolationMode), code) ? (InterpolationMode)code : InterpolationMode.Bicubic;
        }
        public static bool GetGaussianBlur() => GetValue(Keys.GaussianBlur, Defaults.GaussianBlur);
        public static bool SetGaussianBlur(bool value) => SetValue(Keys.GaussianBlur, value);

        // 更新检查相关
        public static bool GetAutoUpdateCheckEnable() => GetValue(Keys.AutoUpdateCheck, Defaults.AutoUpdateCheck);
        public static bool SetAutoUpdateCheckEnable(bool value) => SetValue(Keys.AutoUpdateCheck, value);

        // 获取配置值
        private static T GetValue<T>(string key, T defaultValue)
        {
            if (_localSettings.Values.TryGetValue(key, out object? value))
            {
                try
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                catch
                {
                    return defaultValue;
                }
            }
            return defaultValue;
        }

        // 设置配置值
        private static bool SetValue(string key, object value)
        {
            if (string.IsNullOrEmpty(key)) return false;

            Type type = value.GetType();
            if (!IsSupportedType(type))
            {
                return false;
            }

            try
            {
                _localSettings.Values[key] = value;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // 检查值类型是否支持
        private static bool IsSupportedType(Type type)
        {
            return type.IsPrimitive
                   || type == typeof(string)
                   || type == typeof(DateTime)
                   || type == typeof(DateTimeOffset)
                   || type == typeof(TimeSpan)
                   || type == typeof(Guid);
        }
    }
}
